import request from "../utils/request"
import { get_user_name } from "../utils/decode_jwt"
import { graphql_request } from "../utils/some_useful"

export function addTodo(content) {
  var user_name = get_user_name()
  var data = `mutation{
    addTodo(input:[
      {
        content: "${content}",
        done: false,
        owner: {name:"${user_name}"}
      }
    ]) {
      todo {
        id
        content
        done
      }
    }
  }`

  return request(graphql_request(data))
}

export function queryAllTodo() {
  //var user_name = get_user_name()
  var data = `query{
    queryTodo() {
      id
      content
      done
    }
  }`

  return request(graphql_request(data))
}

export function deleteTodo(id) {
  // as below, the 'id' in 'filter' must in [], and todo{id} can't be just id
  var data = `mutation{
    deleteTodo(filter: {id: ["${id}"]}) {
      todo {
        id
      }
    }
  }`
  return request(graphql_request(data))
}

export function doneTodo(id, done) {
  // I found that I can't use graphql variables in js,so I use this type to instead of
  var input = `
  {
    filter:{id: ["${id}"]},
    set: {done: ${done}}
  }
  `
  var data = `mutation{
    updateTodo(input:${input}) {
      todo {
        id
        done
      }
    }
  }`
  return request(graphql_request(data))
}
